package cc.yaomm.service.basic.controller.business.user.biz;

import cc.yaomm.common.basic.util.BeanUtils;
import cc.yaomm.common.biz.enums.OwnerTypeEnum;
import cc.yaomm.service.basic.controller.business.BaseAdminBiz;
import cc.yaomm.service.basic.controller.business.user.vo.PermissionDetailResultVO;
import cc.yaomm.service.basic.controller.business.user.vo.PermissionParamVO;
import cc.yaomm.service.basic.controller.business.user.vo.PermissionResultVO;
import cc.yaomm.service.basic.service.user.PermissionService;
import cc.yaomm.service.basic.service.user.bo.PermissionParamBO;
import cc.yaomm.service.basic.service.user.bo.PermissionResultBO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PermissionBiz extends BaseAdminBiz {

    @Autowired
    private PermissionService permissionsService;

    /**
     * 查询权限树形结构
     *
     * @return
     */
    public List<PermissionResultVO> findTree(PermissionParamVO paramVO) {
        List<PermissionResultBO> resultBOTree = permissionsService.findTree(OwnerTypeEnum.BIZ, paramVO.getStatus());
        if (CollectionUtils.isEmpty(resultBOTree)) {
            return new ArrayList<>();
        }
        return BeanUtils.mapList(resultBOTree, PermissionResultVO.class);
    }

    /**
     * 锁定/解锁
     */
    public void lock(PermissionParamVO paramVO) {
        permissionsService.lock(paramVO.getId(), paramVO.getStatus());
    }

    /**
     * 创建/编辑
     *
     * @param paramVO
     */
    public void save(PermissionParamVO paramVO) {
        PermissionParamBO paramBO = BeanUtils.map(paramVO, PermissionParamBO.class);
        paramBO.setOwnerType(OwnerTypeEnum.BIZ);
        permissionsService.save(paramBO);
    }

    /**
     * 权限详情
     *
     * @param id
     */
    public PermissionDetailResultVO detail(long id) {
        PermissionResultBO resultBO = permissionsService.detail(id);
        PermissionDetailResultVO resultVO = BeanUtils.map(resultBO, PermissionDetailResultVO.class);
        return resultVO;
    }
}
